//
//  CustomSearchView.m
//  YaoHeAPP
//
//  Created by 杨亚森 on 16/7/27.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "CustomSearchView.h"
#import "ShaiXuanTableViewCell.h"
#import "SouSuoTiaoJianCommonModel.h"
#import "SouSuoTiaoJianResultModel.h"
#import "SouSuoTiaoJianTableViewCell.h"
#import "TongYong.h"
#import "ShowMessageView.h"

#define kCustomShaiXuanView @"kCustomShaiXuanView"

@interface CustomSearchView () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSourceArray;              // tableView数据源
@property (nonatomic, strong) ShaiXuanTableViewCell *shaiXuanTableViewCell; // 复用的cell
@property (nonatomic, strong) NSMutableDictionary *blockDictionary;         // 回调用的字典
@property (nonatomic, strong) NSDictionary *transformDictionary;            // 转换回调字典用的字典
@property (nonatomic, strong) NSMutableSet *zhankaiCellIndexPathSet;        // 显示"展开"cell的索引集合
@property (nonatomic, strong) NSMutableSet *shouqiCellIndexPathSet;         // 显示"收起"cell的索引集合
@property (nonatomic, strong) NSMutableSet *yiXuanIndexPathSet;             // 记住哪行Set
@property (nonatomic, strong) NSIndexPath *multipleChoiceIndexPath;         // 记住哪行
@property (nonatomic, strong) NSString *multipleChoiceYiXuanValue;          // 记住选了哪个
@property (nonatomic, strong) NSMutableDictionary *yiXuanValueDictionary;   // 记住选了哪个放到对应的行号里面

@end

@implementation CustomSearchView

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height-40) style:UITableViewStylePlain];
        tableView.dataSource = self;
        tableView.delegate = self;
        tableView.separatorStyle = NO;
        [self addSubview:tableView];
        self.tableView = tableView;
        
        self.dataSourceArray = [NSMutableArray array];
        self.blockDictionary = [NSMutableDictionary dictionary];
        self.transformDictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"FactoryName",@"生产商", @"MaterialName",@"材质", @"SurfaceName",@"表面", @"ThickName",@"厚度", @"WidthName",@"宽度", @"MarketName",@"市场", @"TypeName",@"品类", nil];
        self.zhankaiCellIndexPathSet = [NSMutableSet set];
        self.shouqiCellIndexPathSet = [NSMutableSet set];
        self.yiXuanIndexPathSet = [NSMutableSet set];
        self.yiXuanValueDictionary = [NSMutableDictionary dictionary];
        
        // 取消按钮
        UIButton *cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(0, tableView.bottom, 100, 40)];
        cancelButton.backgroundColor = RGBCOLOR(251, 239, 211);
        [cancelButton setTitle:@"重置" forState:UIControlStateNormal];
        [cancelButton setTitleColor:RGBCOLOR(252, 123, 96) forState:UIControlStateNormal];
        [cancelButton addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:cancelButton];
        
        UIButton *conformButton = [[UIButton alloc] initWithFrame:CGRectMake(100, tableView.bottom, self.width-100, 40)];
        conformButton.backgroundColor = RGBCOLOR(251, 87, 60);
        [conformButton setTitle:@"确认" forState:UIControlStateNormal];
        [conformButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [conformButton addTarget:self action:@selector(conformAction) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:conformButton];
    }
    return self;
}

- (void)setSouSuoTiaoJianResultModel:(SouSuoTiaoJianResultModel *)souSuoTiaoJianResultModel {
    _souSuoTiaoJianResultModel = souSuoTiaoJianResultModel;;
    
    [self.dataSourceArray removeAllObjects];
    
    NSArray *categoryArray = souSuoTiaoJianResultModel.CATEGORY;
    if (categoryArray && categoryArray.count > 0) {
        NSDictionary *tempDictionary = [NSDictionary dictionaryWithObject:categoryArray forKey:@"品类"];
        [self.dataSourceArray addObject:tempDictionary];
    }
    NSArray *marketArray = souSuoTiaoJianResultModel.MARKET;
    if (marketArray && marketArray.count > 0) {
        NSDictionary *tempDictionary = [NSDictionary dictionaryWithObject:marketArray forKey:@"市场"];
        [self.dataSourceArray addObject:tempDictionary];
    }
    NSArray *matericalArray = souSuoTiaoJianResultModel.MATERICAL;
    if (matericalArray && matericalArray.count > 0) {
        NSDictionary *tempDictionary = [NSDictionary dictionaryWithObject:matericalArray forKey:@"材质"];
        [self.dataSourceArray addObject:tempDictionary];
    }
    NSArray *surfaceArray = souSuoTiaoJianResultModel.SURFACE;
    if (surfaceArray && surfaceArray.count > 0) {
        NSDictionary *tempDictionary = [NSDictionary dictionaryWithObject:surfaceArray forKey:@"表面"];
        [self.dataSourceArray addObject:tempDictionary];
    }
    NSArray *widthArray = souSuoTiaoJianResultModel.WIDTH;
    if (widthArray && widthArray.count > 0) {
        NSDictionary *tempDictionary = [NSDictionary dictionaryWithObject:widthArray forKey:@"宽度"];
        [self.dataSourceArray addObject:tempDictionary];
    }
    NSArray *thickinessArray = souSuoTiaoJianResultModel.THICKINESS;
    if (thickinessArray && thickinessArray.count > 0) {
        NSDictionary *tempDictionary = [NSDictionary dictionaryWithObject:thickinessArray forKey:@"厚度"];
        [self.dataSourceArray addObject:tempDictionary];
    }
    NSArray *producterArray = souSuoTiaoJianResultModel.PRODUCTER;
    if (producterArray && producterArray.count > 0) {
        NSDictionary *tempDictionary = [NSDictionary dictionaryWithObject:producterArray forKey:@"生产商"];
        [self.dataSourceArray addObject:tempDictionary];
    }
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableView reloadData];
    });
}

#pragma mark - TableView DataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSourceArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SouSuoTiaoJianTableViewCell *cell = [[SouSuoTiaoJianTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCustomShaiXuanView];
    NSDictionary *tempModelDictionary = self.dataSourceArray[indexPath.row];
    if ([tempModelDictionary.allKeys.lastObject isEqualToString:@"生产商"]) {
        cell.selectionMode = multipleChoice;
    } else {
        cell.selectionMode = radio;
    }
    if ([self.yiXuanIndexPathSet containsObject:indexPath]) {
        cell.yiXuanValue = [self.yiXuanValueDictionary objectForKey:indexPath];
    }
    if (self.multipleChoiceIndexPath == indexPath) {
        cell.multipleChoiceYiXuanValue = self.multipleChoiceYiXuanValue;
    }
    cell.modelDictionary = tempModelDictionary;
    if ([self.zhankaiCellIndexPathSet containsObject:indexPath]) {
        cell.moreButton.selected = YES;
    }
    if ([self.shouqiCellIndexPathSet containsObject:indexPath]) {
        cell.moreButton.selected = NO;
    }
    
    cell.radioSelectButtonClickBlock = ^(NSString *key, NSString *value) {
        if (key != nil && value != nil) {
            [self.yiXuanIndexPathSet addObject:indexPath];
            [self.yiXuanValueDictionary setObject:value forKey:indexPath];
            [self.blockDictionary setObject:value forKey:self.transformDictionary[key]];
        } else {
            [self.yiXuanIndexPathSet removeObject:indexPath];
            [self.yiXuanValueDictionary removeObjectForKey:indexPath];
            [self.blockDictionary removeObjectForKey:self.transformDictionary[key]];
        }
    };
    cell.multipleChoiceSelectButtonClickBlock = ^(NSString *key, NSString *value) {
        if (key != nil && value != nil) {
            self.multipleChoiceIndexPath = indexPath;
            self.multipleChoiceYiXuanValue = value;
            [self.blockDictionary setObject:value forKey:self.transformDictionary[key]];
        } else {
            self.multipleChoiceIndexPath = nil;
            self.multipleChoiceYiXuanValue = @"";
            [self.blockDictionary removeObjectForKey:self.transformDictionary[key]];
        }
    };
    cell.zhanKaiReloadIndexPathCellBlock = ^() {
        [self.zhankaiCellIndexPathSet addObject:indexPath];
        if ([self.shouqiCellIndexPathSet containsObject:indexPath]) {
            [self.shouqiCellIndexPathSet removeObject:indexPath];
        }
        [tableView reloadData];
    };
    cell.shouQiReloadIndexPathCellBlock = ^() {
        [self.shouqiCellIndexPathSet addObject:indexPath];
        if ([self.zhankaiCellIndexPathSet containsObject:indexPath]) {
            [self.zhankaiCellIndexPathSet removeObject:indexPath];
        }
        [tableView reloadData];
    };
    // 此属性用完清空
    cell.yiXuanValue = nil;
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.zhankaiCellIndexPathSet containsObject:indexPath]) {
        int index = (int)indexPath.row;
        NSDictionary *tempModelDictionary = self.dataSourceArray[index];
        return [self.shaiXuanTableViewCell getCellHeightWithShaiXuanTiaoJianCommonModelDictionary:tempModelDictionary];
    }
    if ([self.shouqiCellIndexPathSet containsObject:indexPath]) {
        return kShaiXuanTableViewCellHeight;
    }
    return kShaiXuanTableViewCellHeight;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - tableView cell
- (ShaiXuanTableViewCell *)shaiXuanTableViewCell {
    if (!_shaiXuanTableViewCell) {
        _shaiXuanTableViewCell = [[ShaiXuanTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCustomShaiXuanView];
    }
    return _shaiXuanTableViewCell;
}

#pragma mark - 按钮点击事件
- (void)cancelAction {
    [self.blockDictionary removeAllObjects];
    [self.zhankaiCellIndexPathSet removeAllObjects];
    [self.shouqiCellIndexPathSet removeAllObjects];
    [self.yiXuanIndexPathSet removeAllObjects];
    [self.yiXuanValueDictionary removeAllObjects];
    self.multipleChoiceIndexPath = nil;
    self.multipleChoiceYiXuanValue = nil;
    
    [self.tableView reloadData];
}

- (void)conformAction {
    // 判空
    BOOL result = [self pankong];
    if (result == NO) return;
    
    [self.zhankaiCellIndexPathSet removeAllObjects];
    [self.shouqiCellIndexPathSet removeAllObjects];
    if (self.customSearchViewCancelOrConformBlock) {
        self.customSearchViewCancelOrConformBlock(self.blockDictionary);
    }
}

#pragma mark 判断每个必填项是否为空
- (BOOL)pankong {
    // 品类
    NSString *pinleiString = self.blockDictionary[@"TypeName"];
    if ([TongYong isBlankObjectAndStr:pinleiString]) {
        [ShowMessageView showMessage:@"请选择品类"];
        return NO;
    }
    // 市场
    NSString *shichangString = self.blockDictionary[@"MarketName"];
    if ([TongYong isBlankObjectAndStr:shichangString]) {
        [ShowMessageView showMessage:@"请选择市场"];
        return NO;
    }
    // 材质
    NSString *caizhiString = self.blockDictionary[@"MaterialName"];
    if ([TongYong isBlankObjectAndStr:caizhiString]) {
        [ShowMessageView showMessage:@"请选择材质"];
        return NO;
    }
    // 表面
    NSString *biaomianString = self.blockDictionary[@"SurfaceName"];
    if ([TongYong isBlankObjectAndStr:biaomianString]) {
        [ShowMessageView showMessage:@"请选择表面"];
        return NO;
    }
    // 宽度
    NSString *kuanduString = self.blockDictionary[@"WidthName"];
    if ([TongYong isBlankObjectAndStr:kuanduString]) {
        [ShowMessageView showMessage:@"请选择宽度"];
        return NO;
    }
    // 生产商
    NSString *shengchanshangString = self.blockDictionary[@"FactoryName"];
    if ([TongYong isBlankObjectAndStr:shengchanshangString]) {
        [ShowMessageView showMessage:@"请选择生产商"];
        return NO;
    }
    
    return YES;
}

@end
